
function externalInit() {
	getDataset("userds").initialize();
	getDataset("userdsright").initialize();
	initDefaultDsData();
	getComponent("ltor").formRightData = function() {
		var userds = getDataset("userds");
		var userdsRight = getDataset("userdsright");
		var rows = userds.getSelectedRows();
		var addRows = new Array;
		if(rows != null) {
			for(var i = 0; i < rows.length; i ++) {
				if(rows[i].getCellValue(7) == "0"){
					var row = rows[i];
					var addRow = userdsRight.getEmptyRow();
					addRow.setCellValue(0, row.getCellValue(0));
					addRow.setCellValue(1, row.getCellValue(1));
					addRow.setCellValue(2, row.getCellValue(2));
					addRow.setCellValue(3, row.getCellValue(3));
					addRow.setCellValue(4, row.getCellValue(4));
					addRow.setCellValue(5, row.getCellValue(5));
					addRow.setCellValue(6, row.getCellValue(6));
					addRows.push(addRow);
				}
			}
		}
		return addRows;
	};
}
function initDefaultDsData(){
	var obj = parent.window.assignparams;
	if(obj == null)
	    obj = parent.parent.window.assignparams;
	if(obj == null)
		return;
	var assign_str = obj.toString();
	var dom = createXmlDom(assign_str);
	var rootNode = dom.documentElement;
	var roleList = rootNode.childNodes;
	var ds = getDataset("userds");
	var rowArr = new Array;
	if( roleList != null && roleList.length > 0){
		for(var i = 0; i < roleList.length; i ++){
			var role = roleList[i];
			var activeid = role.getAttribute("activeid");
			var rolename = role.getAttribute("name");
			addRole(ds, activeid,rolename, rowArr);
			var userList = role.childNodes;
			if(userList != null && userList.length > 0){
				for(var k=0;k < userList.length; k++){
					var user = userList[k];
					addUser(ds, activeid,user.childNodes, rowArr);
				}
			}
		}
	}
	ds.silent = true;
	ds.insertRows(0, rowArr, false);
	ds.silent = false;
	ds.setCurrentPage(ds.currentKey);
	return ds;
}

function addRole(ds, activeid,rolename, rowArr){
	var row = ds.getEmptyRow();
	row.setCellValue(0, activeid);
	row.setCellValue(1, rolename);
	row.setCellValue(2, rolename);
	//usertype
	row.setCellValue(6, "1");
	rowArr.push(row);
}
function addUser(ds, activeid,nodes, rowArr){
	var row = ds.getEmptyRow();
	row.setCellValue(0,activeid + nodes[0].text);
	row.setCellValue(1,nodes[0].text);
	
	row.setCellValue(2,nodes[2].text);
	row.setCellValue(3,nodes[1].text);
	
	row.setCellValue(4,nodes[3].text);
	row.setCellValue(5,nodes[4].text);
	row.setCellValue(6,activeid);
	row.setCellValue(7,"0");
	rowArr.push(row);
}
function okForAssignFunction(){
	var rightDs = getDataset("userdsright");
	var count = rightDs.getRowCount();
	if(count == 0){
		showMessageDialog("请指派流程下已环节的参与者!");
		return ;
	}
	//gh 20141022增加对于来源单据提交和审批的限制区分，事项单走的为系统内置方法。
	var fromtype = parent.getFromtype();
	var pksArr = getAssignMap();
	var type = getParameter("action");
	if(fromtype !=null && "undefined" != typeof(fromtype) && fromtype =="todolist"){
		if(type == 'approve'){
			parent.setAssPks2ApprovePage(pksArr);
		}else if(type == "commit"){
			parent.billUI.eventHandler.boCommit(pksArr);
		}
	}else{
		if(type == 'approve'){
			//如果是审批时需要指派，则将选择的pk放入window中
			parent.setAssPks2ApprovePage(pksArr);
		}else if(type == "commit"){
			//如果是保存提交时，则直接继续提交
			parent.billUI.eventHandler.JkbxSaveImpl(pksArr);
		}
	}
	
	parent.hideDialog("assignable_info_dialog");
}

function getAssignMap(){
	var obj = "{javaClass:'java.util.HashMap',map:{";
	
	var rightDs = getDataset("userdsright");
	var count = rightDs.getRowCount();
	
	var map = new HashMap();
	for(var i=0; i<count; i++){
		var pk_user = rightDs.getRow(i).getCellValue(1);
		var activeid = rightDs.getRow(i).getCellValue(6);
		if(!map.containsKey(activeid)){
			var arr = new Array();
			arr.push(pk_user);
			map.put(activeid, arr);
		}else{
			var arr = map.get(activeid);
			arr.push(pk_user);
			map.put(activeid, arr);
		}
	}
	
	var keySet = map.keySet();
	for(var j = 0, len = keySet.length; j < len; j++ ){
		var arr = map.get(keySet[j]);
		obj += keySet[j] + ":'" + arr.join('$') + "'" ;
		if(j != len-1)
			obj += ",";
	}
	obj += "}}";
	return obj;
}
